package com.zhongshengnetwork.rightbe.lang.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.Filter.FilterColorMatrix;
import com.zhongshengnetwork.rightbe.common.Filter.FilterTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.lang.res.Configs;

public class FilterActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {

    private TopBarView topbar;

    private ImageView img;

    private int screenWidth = 0;

    private LinearLayout f0_layout;
    private ImageView f0;
    private ImageView s0;
    private TextView f0_text;

    private LinearLayout f1_layout;
    private ImageView f1;
    private ImageView s1;
    private TextView f1_text;

    private LinearLayout f2_layout;
    private ImageView f2;
    private ImageView s2;
    private TextView f2_text;

    private LinearLayout f3_layout;
    private ImageView f3;
    private ImageView s3;
    private TextView f3_text;

    private LinearLayout f4_layout;
    private ImageView f4;
    private ImageView s4;
    private TextView f4_text;

    private LinearLayout f5_layout;
    private ImageView f5;
    private ImageView s5;
    private TextView f5_text;

    private LinearLayout f6_layout;
    private ImageView f6;
    private ImageView s6;
    private TextView f6_text;

    private LinearLayout f7_layout;
    private ImageView f7;
    private ImageView s7;
    private TextView f7_text;

    private LinearLayout f8_layout;
    private ImageView f8;
    private ImageView s8;
    private TextView f8_text;

    private LinearLayout f9_layout;
    private ImageView f9;
    private ImageView s9;
    private TextView f9_text;

    private LinearLayout f10_layout;
    private ImageView f10;
    private ImageView s10;
    private TextView f10_text;

    private LinearLayout f11_layout;
    private ImageView f11;
    private ImageView s11;
    private TextView f11_text;

    private LinearLayout f12_layout;
    private ImageView f12;
    private ImageView s12;
    private TextView f12_text;

    private LinearLayout f13_layout;
    private ImageView f13;
    private ImageView s13;
    private TextView f13_text;

    private int currentSelect = 0;

    private Bitmap originBitmap = null;
    private Bitmap filterBitmap = null;

    private void selectClick(int select){
        if (select != currentSelect){
            if (currentSelect == 0){
                f0_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s0.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 1){
                f1_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s1.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 2){
                f2_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s2.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 3){
                f3_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s3.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 4){
                f4_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s4.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 5){
                f5_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s5.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 6){
                f6_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s6.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 7){
                f7_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s7.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 8){
                f8_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s8.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 9){
                f9_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s9.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 10){
                f10_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s10.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 11){
                f11_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s11.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 12){
                f12_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s12.setVisibility(View.INVISIBLE);
            }else if (currentSelect == 13){
                f13_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                s13.setVisibility(View.INVISIBLE);
            }

            if (select == 0){
                f0_text.setTextColor(Color.parseColor("#FF99CC00"));
                s0.setVisibility(View.VISIBLE);
            }else if (select == 1){
                f1_text.setTextColor(Color.parseColor("#FF99CC00"));
                s1.setVisibility(View.VISIBLE);
            }else if (select == 2){
                f2_text.setTextColor(Color.parseColor("#FF99CC00"));
                s2.setVisibility(View.VISIBLE);
            }else if (select == 3){
                f3_text.setTextColor(Color.parseColor("#FF99CC00"));
                s3.setVisibility(View.VISIBLE);
            }else if (select == 4){
                f4_text.setTextColor(Color.parseColor("#FF99CC00"));
                s4.setVisibility(View.VISIBLE);
            }else if (select == 5){
                f5_text.setTextColor(Color.parseColor("#FF99CC00"));
                s5.setVisibility(View.VISIBLE);
            }else if (select == 6){
                f6_text.setTextColor(Color.parseColor("#FF99CC00"));
                s6.setVisibility(View.VISIBLE);
            }else if (select == 7){
                f7_text.setTextColor(Color.parseColor("#FF99CC00"));
                s7.setVisibility(View.VISIBLE);
            }else if (select == 8){
                f8_text.setTextColor(Color.parseColor("#FF99CC00"));
                s8.setVisibility(View.VISIBLE);
            }else if (select == 9){
                f9_text.setTextColor(Color.parseColor("#FF99CC00"));
                s9.setVisibility(View.VISIBLE);
            }else if (select == 10){
                f10_text.setTextColor(Color.parseColor("#FF99CC00"));
                s10.setVisibility(View.VISIBLE);
            }else if (select == 11){
                f11_text.setTextColor(Color.parseColor("#FF99CC00"));
                s11.setVisibility(View.VISIBLE);
            }else if (select == 12){
                f12_text.setTextColor(Color.parseColor("#FF99CC00"));
                s12.setVisibility(View.VISIBLE);
            }else if (select == 13){
                f13_text.setTextColor(Color.parseColor("#FF99CC00"));
                s13.setVisibility(View.VISIBLE);
            }

            currentSelect = select;
            img.setImageBitmap(FilterTools.toProcessImage(originBitmap, currentSelect));

        }
    }


    private void calculateImageWidth(){
        int width = ScreenUtils.getScreenWidth(FilterActivity.this);
        screenWidth = width;
        int imgH = Math.round(screenWidth / Configs.Value.IMAGE_WIDTH_HEIGHT_RATIO);
        if (img != null){
            ViewGroup.LayoutParams params = img.getLayoutParams();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            img.setLayoutParams(params);
        }


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        calculateImageWidth();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);

        topbar.getRightTextView().setText("保存");
        topbar.getRightTextView().setTextSize(18);
        topbar.getRightTextView().setTextColor(getResources().getColor(R.color.mainColor));
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("filterIndex", currentSelect);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        RelativeLayout filter_layout = (RelativeLayout) findViewById(R.id.filter_layout);
        filter_layout.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());

        HorizontalScrollView filter_menu = (HorizontalScrollView) findViewById(R.id.filter_menu);
        filter_menu.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

        img = (ImageView) findViewById(R.id.img);
        calculateImageWidth();

        Uri uri = (Uri)getIntent().getParcelableExtra("Uri");
        originBitmap = BitmapFactory.decodeFile(uri.getEncodedPath());
        if (originBitmap != null){
            img.setImageBitmap(originBitmap);
        }

        f0_layout = (LinearLayout) findViewById(R.id.f0_layout);
        f0_layout.setTag(0);
        f0_layout.setClickable(true);
        f0_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f0 = (ImageView) findViewById(R.id.f0);
        s0 = (ImageView) findViewById(R.id.s0);
        f0_text = (TextView) findViewById(R.id.f0_text);
        f0_text.setTextColor(Color.parseColor("#FF99CC00"));

        f1_layout = (LinearLayout) findViewById(R.id.f1_layout);
        f1_layout.setTag(1);
        f1_layout.setClickable(true);
        f1_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f1 = (ImageView) findViewById(R.id.f1);
        s1 = (ImageView) findViewById(R.id.s1);
        f1_text = (TextView) findViewById(R.id.f1_text);
        f1_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f2_layout = (LinearLayout) findViewById(R.id.f2_layout);
        f2_layout.setTag(2);
        f2_layout.setClickable(true);
        f2_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f2 = (ImageView) findViewById(R.id.f2);
        s2 = (ImageView) findViewById(R.id.s2);
        f2_text = (TextView) findViewById(R.id.f2_text);
        f2_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f3_layout = (LinearLayout) findViewById(R.id.f3_layout);
        f3_layout.setTag(3);
        f3_layout.setClickable(true);
        f3_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f3 = (ImageView) findViewById(R.id.f3);
        s3 = (ImageView) findViewById(R.id.s3);
        f3_text = (TextView) findViewById(R.id.f3_text);
        f3_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f4_layout = (LinearLayout) findViewById(R.id.f4_layout);
        f4_layout.setTag(4);
        f4_layout.setClickable(true);
        f4_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f4 = (ImageView) findViewById(R.id.f4);
        s4 = (ImageView) findViewById(R.id.s4);
        f4_text = (TextView) findViewById(R.id.f4_text);
        f4_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f5_layout = (LinearLayout) findViewById(R.id.f5_layout);
        f5_layout.setTag(5);
        f5_layout.setClickable(true);
        f5_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f5 = (ImageView) findViewById(R.id.f5);
        s5 = (ImageView) findViewById(R.id.s5);
        f5_text = (TextView) findViewById(R.id.f5_text);
        f5_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f6_layout = (LinearLayout) findViewById(R.id.f6_layout);
        f6_layout.setTag(6);
        f6_layout.setClickable(true);
        f6_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f6 = (ImageView) findViewById(R.id.f6);
        s6 = (ImageView) findViewById(R.id.s6);
        f6_text = (TextView) findViewById(R.id.f6_text);
        f6_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f7_layout = (LinearLayout) findViewById(R.id.f7_layout);
        f7_layout.setTag(7);
        f7_layout.setClickable(true);
        f7_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f7 = (ImageView) findViewById(R.id.f7);
        s7 = (ImageView) findViewById(R.id.s7);
        f7_text = (TextView) findViewById(R.id.f7_text);
        f7_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f8_layout = (LinearLayout) findViewById(R.id.f8_layout);
        f8_layout.setTag(8);
        f8_layout.setClickable(true);
        f8_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f8 = (ImageView) findViewById(R.id.f8);
        s8 = (ImageView) findViewById(R.id.s8);
        f8_text = (TextView) findViewById(R.id.f8_text);
        f8_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f9_layout = (LinearLayout) findViewById(R.id.f9_layout);
        f9_layout.setTag(9);
        f9_layout.setClickable(true);
        f9_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f9 = (ImageView) findViewById(R.id.f9);
        s9 = (ImageView) findViewById(R.id.s9);
        f9_text = (TextView) findViewById(R.id.f9_text);
        f9_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f10_layout = (LinearLayout) findViewById(R.id.f10_layout);
        f10_layout.setTag(10);
        f10_layout.setClickable(true);
        f10_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f10 = (ImageView) findViewById(R.id.f10);
        s10 = (ImageView) findViewById(R.id.s10);
        f10_text = (TextView) findViewById(R.id.f10_text);
        f10_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f11_layout = (LinearLayout) findViewById(R.id.f11_layout);
        f11_layout.setTag(11);
        f11_layout.setClickable(true);
        f11_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f11 = (ImageView) findViewById(R.id.f11);
        s11 = (ImageView) findViewById(R.id.s11);
        f11_text = (TextView) findViewById(R.id.f11_text);
        f11_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f12_layout = (LinearLayout) findViewById(R.id.f12_layout);
        f12_layout.setTag(12);
        f12_layout.setClickable(true);
        f12_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f12 = (ImageView) findViewById(R.id.f12);
        s12 = (ImageView) findViewById(R.id.s12);
        f12_text = (TextView) findViewById(R.id.f12_text);
        f12_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        f13_layout = (LinearLayout) findViewById(R.id.f13_layout);
        f13_layout.setTag(13);
        f13_layout.setClickable(true);
        f13_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick((int)view.getTag());
            }
        });
        f13 = (ImageView) findViewById(R.id.f13);
        s13 = (ImageView) findViewById(R.id.s13);
        f13_text = (TextView) findViewById(R.id.f13_text);
        f13_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        Intent intent = new Intent();
        intent.putExtra("filterIndex", 0);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

}
